from django.db.models import F
from apps.user.models import UserModel


# 更新团队成员营业额
def updateTeamPerformance(scene, parent, n):
    print('更新', scene, parent.id, n)
    if parent.id:
        # 更新团队总人数
        _parent = UserModel.objects.get(id=parent.id)
        if scene == 'add':
            _parent.teamPerformance = F('teamPerformance') + n
            _parent.save()
        elif scene == 'sub':
            if _parent.teamPerformance >= n:
                _parent.teamPerformance = F('teamPerformance') - n
                _parent.save()
            else:
                _parent.teamPerformance = 0
                _parent.save()

        if _parent.parent:
            updateTeamPerformance(scene, _parent.parent, n)
    else:
        return False


# 更新团队人数
def updateTeamPeopleNumber(scene, parent, n):
    print('更新', scene, parent.id, n)
    if parent.id:
        # 更新团队总人数
        _parent = UserModel.objects.get(id=parent.id)
        if scene == 'add':
            _parent.teamPeopleNumber = F('teamPeopleNumber') + n
            _parent.save()
        elif scene == 'sub':
            if _parent.teamPeopleNumber >= n:
                _parent.teamPeopleNumber = F('teamPeopleNumber') - n
                _parent.save()
            else:
                _parent.teamPeopleNumber = 0
                _parent.save()

        if _parent.parent:
            updateTeamPeopleNumber(scene, _parent.parent, n)
    else:
        return False


# 更新团队订单数
def updateTeamOrderNumber(scene, parent, n):
    print('更新', scene, parent.id, n)
    if parent.id:
        # 更新团队总人数
        _parent = UserModel.objects.get(id=parent.id)
        if scene == 'add':
            _parent.teamOrderNumber = F('teamOrderNumber') + n
            _parent.save()
        elif scene == 'sub':
            if _parent.teamOrderNumber >= n:
                _parent.teamOrderNumber = F('teamOrderNumber') - n
                _parent.save()
            else:
                _parent.teamOrderNumber = 0
                _parent.save()

        if _parent.parent:
            updateTeamOrderNumber(scene, _parent.parent, n)
    else:
        return False